package com.tang.controller;

import com.tang.config.BaseContext;
import com.tang.entity.ConfirmDTO;
import com.tang.entity.Food;
import com.tang.entity.XuetangDTO;
import com.tang.pojo.*;
import com.tang.service.CommonService;
import com.tang.service.FoodService;
import com.tang.service.TakeFoodService;
import com.tang.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.common.support.IController;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务控制器
 *
 * @author kancy
 * @since 2024-07-21 14:12:37
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户相关接口")
public class UserController implements IController {

    @Autowired
    FoodService foodService;
    @Autowired
    TakeFoodService takeFoodService;
    @Autowired
    CommonService commonService;

    @Autowired
    UserService userService;



    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO){
        //TODO 令牌 token登录 微信校验
        return userService.login(userLoginDTO);

    }

    @ApiOperation("用户首页展示内容")
    @GetMapping("/index") //TODO 返回当天目前摄入的糖类脂肪 蛋白质 热量 和血糖测量情况
    public Result<IndexVO> index(){
        log.info("userid:"+String.valueOf(BaseContext.getCurrentId()));

        return Result.success(commonService.index());
    }

    @ApiOperation("更改用户名")
    @PutMapping("/name/{username}")
    public Result<String> changeName(@PathVariable String username){
        return userService.setName(username);
    }

    @ApiOperation("用户记录血糖")
    @PutMapping("/xuetang")
    public Result record(@RequestBody XuetangDTO dto){
        commonService.record(dto.record);
        return Result.success();
    }

    @ApiOperation("返回食物list")
    @GetMapping("/search/list")  //TODO 返回所有食物 使用redis进行缓存
    public List<Food> foodList(){
        return  foodService.list();
    }


    @ApiOperation("搜索食物名称")
    @GetMapping("/search/{foodName}")  //在redis中寻找
    public Result<List<Food>> getByName(@PathVariable String foodName)
    {
        return foodService.getByFoodName(foodName);
    }


    @ApiOperation("记录用户进食")
    @PutMapping("/addfood")
    public Result addFood(@RequestBody AddFoodDTO addFoodDto)
    {
        return takeFoodService.addFood(addFoodDto.getFoodId(),addFoodDto.getCount(),addFoodDto.getType());
    }

    @ApiOperation("拍照上传验证")
    @PostMapping("/confirm")
    public Result confim(@RequestBody ConfirmDTO dto){
        return commonService.confirm(dto);

    }


    @ApiOperation("用户上传菜品照片")
    @PostMapping("/updata")
    public Result<String> updata(@RequestBody PhotoDTO photo){
        return commonService.updata(photo.getPhoto());
    }


    @ApiOperation("展现用户当天进食情况")
    @GetMapping("/typedish")
    public Result<TypedishVo> getTypedish(){
        return takeFoodService.getTypedish();
    }

    @ApiOperation("调用ai")
    @GetMapping("/chatai")
    public Result<String> chatai(String question){
        return commonService.chat(question);
    }

    @ApiOperation("用户查看历史记录")
    @GetMapping("/history")
    public Result<HistoryVO> getHistory(){
        return commonService.getHistory();
    }








}